/* eslint-disable */
import Vue from "vue";
import VueRouter from "vue-router";
import baseLayout from "@/layout/baseLayout";

Vue.use(VueRouter);
//自注册路由   遵循文件夹=>文件名的顺序
const dynamicRoutes = [
  {
    path: "",
    meta: {
      title: "代码审核",
      key: "codeReview",
      icon: "el-icon-menu"
    },
    component: baseLayout,
    children: []
  },
  {
    path: "",
    meta: {
      title: "测试",
      key: "test",
      icon: "el-icon-s-tools"
    },
    component: baseLayout,
    children: []
  }
];

const files = require.context("@/views", true, /\.vue$/);
console.log("files", files[0]);

//排除某些文件夹 默认components文件夹
const noMatched = ["components"];
files.keys().forEach((key) => {
  if (noMatched.some((v) => key.indexOf(v) > 0)) return;
  const filePath = key.replace(/^\.\//, "").replace(/\.vue$/, "");
  const routePath = `/${filePath}`;
  dynamicRoutes.forEach((item) => {
    if (routePath.indexOf(item.meta.key) > 0 && item) {
      const name = routePath.split("/").join(".");
      item.children.push({
        name,
        path: routePath,
        meta: {
          title: "测试",
          key: name.split(".")[
            name.split(".").length >= 4
              ? name.split(".").length - 2
              : name.split(".").length - 1
          ],
          icon: "el-icon-s-tools"
        },
        component: () => import(`@/views/${filePath}`)
      });
    }
  });
});
console.log("dynamicRoutes", dynamicRoutes);

const routes = [
  {
    path: "/",
    redirect: "/codeReview/codeReview01"
  },
  {
    path: "/404",
    component: () => import("@/error-page/404"),
    name: "404",
    meta: {
      keepAlive: true,
      isTab: false,
      isAuth: false
    }
  },
  {
    path: "/403",
    component: () => import("@/error-page/403"),
    name: "403",
    meta: {
      keepAlive: true,
      isTab: false,
      isAuth: false
    }
  },
  {
    path: "/500",
    component: () => import("@/error-page/500"),
    name: "500",
    meta: {
      keepAlive: true,
      isTab: false,
      isAuth: false
    }
  },
  ...dynamicRoutes,
  { path: "*", redirect: "/404" }
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes
});
export { dynamicRoutes };
export default router;
